﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using NUnit.Framework;
using QuickBencher.Repositories;

namespace QuickBencher.Test.Repositories
{
    public class MeasurementRepositoryTests
    {
        [TestFixture]
        public class When_save
        {
            private string measurementDataFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
                "QuickBencher\\measurements.quickbencher");

            [SetUp]
            public void Setup()
            {
				if (File.Exists(measurementDataFile))
	                File.Delete(measurementDataFile);
            }

            [Test]
            public void Then_assure_Data_file_is_stored_under_ApplicationData()
            {
                var repository = new MeasurementRepository();
                repository.Save(new Measurement());

                Assert.IsTrue(File.Exists(measurementDataFile));
            }

            [Test]
            public void Then_measurement_is_persisted()
            {
                var measurementsInput = CreateMeasurements();
            	var repository = new MeasurementRepository();

                foreach (var measurement in measurementsInput)
                {
                    repository.Save(measurement);
                }

                var measurements = repository.GetAll();
                Assert.AreEqual(2, measurements.Count());

            	foreach (var measurement in measurementsInput)
            	{
            		var label = measurement.Label;
            		var persistedMeasurement = measurements.SingleOrDefault(m => m.Label == label);
					Assert.IsNotNull(persistedMeasurement);
					Assert.AreEqual(measurement.RealTime, persistedMeasurement.RealTime);
					Assert.AreEqual(measurement.SystemTime, persistedMeasurement.SystemTime);
					Assert.AreEqual(measurement.TotalTime, persistedMeasurement.TotalTime);
					Assert.AreEqual(measurement.UserTime, persistedMeasurement.UserTime);
            	}
            }

        	private static IEnumerable<Measurement> CreateMeasurements()
        	{
        		var measurementsInput = new List<Measurement>();
        		measurementsInput.Add(new Measurement
        		                      {
        		                      	Label = "hello world",
        		                      	RealTime = 1,
        		                      	SystemTime = 2,
        		                      	TotalTime = 3,
        		                      	UserTime = 4
        		                      });
        		measurementsInput.Add(new Measurement
        		                      {
        		                      	Label = "Testing testing",
        		                      	RealTime = 5,
        		                      	SystemTime = 6,
        		                      	TotalTime = 7,
        		                      	UserTime = 8
        		                      });
        		return measurementsInput;
        	}
        }
    }
}
